package com.itboye.ihomebank.activity.message;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.SystemMessageAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.SystemMessBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.custom.XListView.IXListViewListener;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 系统消息
 * 
 */
public class ActivitySystemMessage extends BaseOtherActivity implements Observer{
	View v_statusbar;
	private ImageView close_icon;// 返回图片
	private TextView add_shap_title_tv;// 标题
	SmartRefreshLayout refresh;
	ListView mess_listview;
	TextView no_mess;

	private UserPresenter userPresenter;
	private KeyPresenter keyPresenter;

	List<SystemMessBean.Sysitem> list = new ArrayList<SystemMessBean.Sysitem>();

	SystemMessageAdapter adapter;
	int type;
	private int pageNo = 1;
	private String uid;
	
	StringBuffer sb;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_system_message;
	}

	@Override
	protected void onInitController() {

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);

		showProgressDialog("数据加载中,请稍后", false);
		add_shap_title_tv.setText(getIntent().getStringExtra("title"));
		uid = (String) SPUtils.get(this, null, SPContants.USER_ID, "");
		type = getIntent().getIntExtra("type", 1);
		
		userPresenter = new UserPresenter(this);// 消息类型【1=>系统消息，2=>预约消息,3=>签约消息,4=>智能锁消息】
		keyPresenter=new KeyPresenter(this);
		caozuo();
		adapter = new SystemMessageAdapter(this, list,R.layout.item_system_message);
		mess_listview.setAdapter(adapter);
		adapter.setType(type);
	}

	private void caozuo(){
		refresh = (SmartRefreshLayout) findViewById(R.id.refresh);
		refresh.setEnableAutoLoadmore(true);//开启自动加载功能（非必须）

		//        //触发自动刷新
		refresh.autoRefresh();
		refresh.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh(RefreshLayout refreshlayout) {
				refreshlayout.getLayout().postDelayed(new Runnable() {
					@Override
					public void run() {
						pageNo = 1;
						userPresenter.getSystemMess(uid, type + "", pageNo + "", "10");
					}
				}, 0);
			}
		});

		//进行加载更多的逻辑处理
		refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
			@Override
			public void onLoadmore(RefreshLayout refreshlayout) {
				refreshlayout.getLayout().postDelayed(new Runnable() {
					@Override
					public void run() {
						pageNo++;
						userPresenter.getSystemMess(uid, type + "", pageNo + "", "10");
					}
				}, 0);
			}
		});

		//设置头布局样式,全局有效
		refresh.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
			@Override
			public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
				//全局设置主题颜色
				layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
				//指定为经典Header，默认是 贝塞尔雷达Header
				return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
			}
		});
		//设置脚布局样式,全局有效
		refresh.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
			@Override
			public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
				//指定为经典Footer，默认是 BallPulseFooter
				return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
			}
		});
		refresh.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
		//  refresh.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
		refresh.finishLoadmore();
	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		// ByAlert.alert(entity.getData());
		if (entity != null) {
			if (!entity.getCode().equals(0)) {
				if (entity.getEventType() == UserPresenter.sysMess_success) {
					SystemMessBean bean = (SystemMessBean) entity.getData();
					//list = bean.getList();
					if (pageNo == 1) {//下拉刷新
						list.clear();
						adapter.notifyDataSetChanged();
						//结束加载
						refresh.finishRefresh();
					} else {//上拉加载
						adapter.notifyDataSetChanged();
						//结束加载
						refresh.finishLoadmore();
					}
					list.addAll(bean.getList());
					if(bean.getList()!=null){
						if (list.size() > 0) {
							mess_listview.setVisibility(View.VISIBLE);
							no_mess.setVisibility(View.GONE);
							adapter.notifyDataSetChanged();
							
							//用下面方法来执行已读
							sb=new StringBuffer();
							for (int i = 0; i < list.size(); i++) {
								list.get(i).getId();
								sb.append(list.get(i).getId()+",");
							}
							String id=sb.substring(0, sb.length()-1).toString();
							keyPresenter.getYiDu(uid, id);
							
						} else {
							mess_listview.setVisibility(View.GONE);
							no_mess.setVisibility(View.VISIBLE);
						}
					}
				}
				if (entity.getEventType() == UserPresenter.sysMess_fail) {
					ByAlert.alert(entity.getData());
					refresh.finishLoadmore();
					refresh.finishRefresh();
				}
				if(entity.getEventType() == KeyPresenter.yidu_success){
					//ByAlert.alert(entity.getData());
				}
				if(entity.getEventType() == KeyPresenter.yidu_fail){
					//ByAlert.alert(entity.getData());
				}
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
